package com.foreknow.service.impl;

import com.foreknow.dao.OrderdetailetDao;
import com.foreknow.dao.OrdersDao;
import com.foreknow.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.dao.impl.OrdersDaoImpl;
import com.foreknow.po.Cart;
import com.foreknow.po.Orderdetailet;
import com.foreknow.po.Orders;
import com.foreknow.service.CartService;
import com.foreknow.service.OrdersService;
import com.foreknow.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public Integer createOrders(String userId, Integer businessId, Integer daId, Double orderTotal) {
        // new一个cart对象，保存userId和businessId，
        // 并且根据userId和businessId查询当前用户购物车中当前商家的所有食品信息(目的要将查询到的结果放到明细中)
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);
        // 创建一个List<Cart>集合，用于保存查询到的当前用户购物车中当前商家的所有食品信息
        List<Cart> list = new ArrayList<>();
        // 创建一个List<Orderdetailet>集合，用于保存需要批量添加的订单详细信息
        List<Orderdetailet> list1 = new ArrayList<>();
        CartService cartService = new CartServiceImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        Integer orderId = 0;
        try {
            DBUtil.beginTransaction();//开启一个事务
            // 1.  查询当前用户购物车中当前商家的所有食品信息(目的要将查询到的结果放到明细中)
            list = cartService.listCart(cart);
            // 2.  创建订单并返回订单的编号
            orderId = ordersDao.saveOrders(userId,businessId,daId,orderTotal);
            // 3.  遍历查询到的购物车信息，并将相关信息保存到List<Orderdetailet>中，便于批量添加
            for(int i = 0;i < list.size();i++){
                Orderdetailet orderdetailet = new Orderdetailet();
//                获取到cart对象中的 foodId和数量
//                创建Orderdetail对象，保存orderId  foodId  quantity
                orderdetailet.setOrderId(orderId);
                orderdetailet.setFoodId(list.get(i).getFoodId());
                orderdetailet.setQuantity(list.get(i).getQuantity());
//                将对象添加到List<Orderdetail>集合中
                list1.add(orderdetailet);
            }
            // 4.  向订单明细表中批量添加数据
            Integer saveorderdetailet = orderdetailetDao.saveOrderdetailet(list1);
            // 5.  清空购物车中相关信息
            cartService.removeCart(cart);
            DBUtil.commitTransaction();//提交一个事务

        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();  //回滚一个事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = new Orders();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        List<Orderdetailet> list = new ArrayList<>();
        try {
            // 先根据orderId查询到订单
            orders = ordersDao.getOrdersById(orderId);
            // 再根据orderId查询订单详细信息
            list = orderdetailetDao.listOrderOrderdetailetByorderId(orderId);
            // 将订单详细存入第一次查询到的订单中
            orders.setOrderdetailetList(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();

        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            // 根据userId查询到用户对应的订单，存入集合中
            list = ordersDao.listOrdersByUserId(userId);
            // 遍历集合，获取到用户对应的每一个订单
            for (Orders orders:list){
                List<Orderdetailet> list1 = new ArrayList<>();
                // 用每个订单对应的orderId查询到对应的订单详细信息，并保存到对应的orders对象中
                list1 = orderdetailetDao.listOrderOrderdetailetByorderId(orders.getOrderId());
                orders.setOrderdetailetList(list1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }
}
